﻿#include <iostream>
#include <vector>
using namespace std;

struct TreeNode 
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
 
class Solution 
{
public:
    // 108. 将有序数组转换为二叉搜索树
    TreeNode* sortedArrayToBST(vector<int>& nums)
    {
        return Conversion(nums, 0, nums.size() - 1);
    }

    TreeNode* Conversion(vector<int>& nums, int left, int right)
    {
        if (left > right)
            return nullptr;

        int mid = (left + right) / 2;
        TreeNode* root = new TreeNode(nums[mid]);
        root->left = Conversion(nums, left, mid - 1);
        root->right = Conversion(nums, mid + 1, right);

        return root;
    }

    // 112. 路径总和
    bool hasPathSum(TreeNode* root, int targetSum)
    {
        if (root == nullptr)
            return false;

        if (root->left == nullptr && root->right == nullptr)
            return root->val == targetSum;

        return hasPathSum(root->left, targetSum - root->val) || hasPathSum(root->right, targetSum - root->val);
    }
};

int main()
{
    vector<int> v{ -10,-3,0,5,9 };
    TreeNode* ret = Solution().sortedArrayToBST(v);

    return 0;
}